<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property string $id
 * @property string $email
 * @property string $username
 * @property string $realname
 * @property string $password
 * @property string $gender
 * @property string $mobile
 * @property string $qq
 * @property double $money
 * @property string $zipcode
 * @property string $address
 * @property string $city_id
 * @property string $enable
 * @property string $manager
 * @property string $create_time
 *
 * The followings are the available model relations:
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username','unique','className'=>'User'),
		    array('email,username, realname, password, repassword', 'required'),
			array('money', 'numerical'),
			array('email','email'),
			array('email', 'length', 'max'=>128),
			array('username, realname, password, repassword', 'length', 'max'=>32),
			array('gender, enable, manager', 'length', 'max'=>1),
			array('mobile, qq', 'length', 'max'=>16),
			array('zipcode', 'length', 'max'=>6),
			array('address', 'length', 'max'=>255),
			array('city_id, create_time', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('password','checkPwd'),
			array('id, email, username, realname, password, gender, mobile, qq, money, zipcode, address, city_id, enable, manager, create_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'city'=>array(self::BELONGS_TO,'Category','city_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'email' => 'Email',
			'username' => '用户名',
			'realname' => '真实姓名',
			'password' => '密码',
			'repassword' => '确认密码',
			'gender' => '性别',
			'mobile' => '电话号码',
			'qq' => 'QQ',
			'money' => 'Money',
			'zipcode' => '邮编',
			'address' => '地址',
			'city_id' => '所在城市',
			'enable' => '可否消费',
			'manager' => '管理员',
			'create_time' => '创建时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('realname',$this->realname,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('gender',$this->gender,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('qq',$this->qq,true);
		$criteria->compare('money',$this->money);
		$criteria->compare('zipcode',$this->zipcode,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('city_id',$this->city_id,true);
		$criteria->compare('enable',$this->enable,true);
		$criteria->compare('manager',$this->manager,true);
		$criteria->compare('create_time',$this->create_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

	 public $repassword;
    function checkPwd()
    {
    	if($this->password!=$this->repassword){
            $this->addError('password','密码错误');
            $this->addError('repassword','两次密码不一致');
    	}
	}


	/*public function validatePassword($password)
    {
        return $this->md5($password)===$this->password;
    }*/


  protected function beforeSave()
{
	$this->create_time=time();


            $this->password = md5($this->password);


        return true;


}

public function validatePassword($password)
    {
        return $this->hashPassword($password)===$this->password;
    }

    public function hashPassword($password)
    {
        return md5($password);
    }

}